/**
 * Copyright 2016 JustWayward Team
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lzy.huijinjiazu.mvp.ui.widget.decoration;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.Gravity;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jess.arms.utils.LogUtils;


public class SpaceDecoration extends RecyclerView.ItemDecoration {

    private int halfSpace;
    private int headerCount = -1;
    private int footerCount = Integer.MAX_VALUE;
    private boolean mPaddingEdgeSide = true;
    private boolean mPaddingStart = true;
    private boolean mPaddingHeaderFooter = false;
    private boolean mPaddingEnd = true;

    private ColorDrawable mColorDrawable = new ColorDrawable(Color.parseColor("#DDDCD7"));


    public SpaceDecoration(int space) {
        this.halfSpace = space / 2;
    }

    public SpaceDecoration(int space, int color, boolean paddingStart, boolean paddingEnd, boolean paddingEdgeSide) {
        this.halfSpace = space / 2;
        this.mColorDrawable = new ColorDrawable(color);
        this.setPaddingStart(paddingStart);
        this.setPaddingEnd(paddingEnd);
        this.setPaddingEdgeSide(paddingEdgeSide);
    }


    /**
     * 边缘线
     *
     * @param mPaddingEdgeSide
     */
    public void setPaddingEdgeSide(boolean mPaddingEdgeSide) {
        this.mPaddingEdgeSide = mPaddingEdgeSide;
    }

    /**
     * 第一一条线
     *
     * @param mPaddingStart
     */
    public void setPaddingStart(boolean mPaddingStart) {
        this.mPaddingStart = mPaddingStart;
    }

    /**
     * 最后一条线
     *
     * @param mPaddingEnd
     */
    public void setPaddingEnd(boolean mPaddingEnd) {
        this.mPaddingEnd = mPaddingEnd;
    }

    /**
     * 头部和尾部的线
     *
     * @param mPaddingHeaderFooter
     */
    public void setPaddingHeaderFooter(boolean mPaddingHeaderFooter) {
        this.mPaddingHeaderFooter = mPaddingHeaderFooter;
    }


    public void setColor(int color) {
        mColorDrawable = new ColorDrawable(color);
    }


    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        int position = parent.getChildAdapterPosition(view);
        int spanCount = 0;
        int orientation = 0;
        int spanIndex = 0;
        int headerCount = 0, footerCount = 0;
        int count = parent.getChildCount();
        boolean isFull = false;
        if (parent.getAdapter() instanceof BaseQuickAdapter) {
            headerCount = ((BaseQuickAdapter) parent.getAdapter()).getHeaderLayoutCount();
            footerCount = ((BaseQuickAdapter) parent.getAdapter()).getFooterLayoutCount();
            count = count - headerCount - footerCount;
        }

        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof StaggeredGridLayoutManager) {
            orientation = ((StaggeredGridLayoutManager) layoutManager).getOrientation();
            spanCount = ((StaggeredGridLayoutManager) layoutManager).getSpanCount();
            spanIndex = ((StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams()).getSpanIndex();
            isFull = ((StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams()).isFullSpan();
        } else if (layoutManager instanceof GridLayoutManager) {
            orientation = ((GridLayoutManager) layoutManager).getOrientation();
            spanCount = ((GridLayoutManager) layoutManager).getSpanCount();
            spanIndex = ((GridLayoutManager.LayoutParams) view.getLayoutParams()).getSpanIndex();
            isFull = false;
        } else if (layoutManager instanceof LinearLayoutManager) {
            orientation = ((LinearLayoutManager) layoutManager).getOrientation();
            spanCount = 1;
            spanIndex = 0;
            isFull = true;
        }

        /**
         * 普通Item的尺寸
         */
        if ((position >= headerCount && position < parent.getAdapter().getItemCount() - footerCount)) {
            int gravity;
            if (isFull && spanIndex == 0) gravity = Gravity.FILL_HORIZONTAL;
            else if (spanIndex == 0 && spanCount > 1) gravity = Gravity.LEFT;
            else if (spanIndex == spanCount - 1 && spanCount > 1) gravity = Gravity.RIGHT;
            else if (spanCount == 1) gravity = Gravity.FILL_HORIZONTAL;
            else {
                gravity = Gravity.CENTER;
            }
            LogUtils.warnInfo(gravity + "=====>>>>" + spanIndex);
            if (orientation == OrientationHelper.VERTICAL) {
                switch (gravity) {
                    case Gravity.LEFT:
                        LogUtils.warnInfo(spanIndex + "=====>>>>gravity:LEFT");
                        if (mPaddingEdgeSide)
                            outRect.left = halfSpace * 2;
                        outRect.right = halfSpace;
                        break;
                    case Gravity.RIGHT:
                        LogUtils.warnInfo(spanIndex + "=====>>>>gravity:RIGHT");
                        outRect.left = halfSpace;
                        if (mPaddingEdgeSide)
                            outRect.right = halfSpace * 2;
                        break;
                    case Gravity.FILL_HORIZONTAL:
                        LogUtils.warnInfo(spanIndex + "=====>>>>gravity:FILL_HORIZONTAL");
                        if (mPaddingEdgeSide) {
                            outRect.left = halfSpace * 2;
                            outRect.right = halfSpace * 2;
                        }
                        break;
                    case Gravity.CENTER:
                        LogUtils.warnInfo(spanIndex + "=====>>>>gravity:CENTER");
                        outRect.left = halfSpace;
                        outRect.right = halfSpace;
                        break;
                }
                if (position - headerCount < spanCount && mPaddingStart)
                    outRect.top = halfSpace * 2;

                if (mPaddingEnd || position - headerCount < count - spanCount)
                    outRect.bottom = halfSpace * 2;
            } else {
                switch (gravity) {
                    case Gravity.LEFT:
                        if (mPaddingEdgeSide)
                            outRect.bottom = halfSpace * 2;
                        outRect.top = halfSpace;
                        break;
                    case Gravity.RIGHT:
                        outRect.bottom = halfSpace;
                        if (mPaddingEdgeSide)
                            outRect.top = halfSpace * 2;
                        break;
                    case Gravity.FILL_HORIZONTAL:
                        if (mPaddingEdgeSide) {
                            outRect.left = halfSpace * 2;
                            outRect.right = halfSpace * 2;
                        }
                        break;
                    case Gravity.CENTER:
                        outRect.bottom = halfSpace;
                        outRect.top = halfSpace;
                        break;
                }
                if (position - headerCount < spanCount && mPaddingStart)
                    outRect.left = halfSpace * 2;
                outRect.right = halfSpace * 2;
            }
        } else {//只有HeaderFooter进到这里
            if (mPaddingHeaderFooter) {//并且需要padding Header&Footer
                if (orientation == OrientationHelper.VERTICAL) {
                    if (mPaddingEdgeSide) {
                        outRect.left = halfSpace * 2;
                        outRect.right = halfSpace * 2;
                    }
                    outRect.top = halfSpace * 2;
                } else {
                    if (mPaddingEdgeSide) {
                        outRect.top = halfSpace * 2;
                        outRect.bottom = halfSpace * 2;
                    }
                    outRect.left = halfSpace * 2;
                }
            }
        }
    }


    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View child = parent.getChildAt(i);
            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
            //垂直
            int left = child.getRight();
            int top = child.getTop() - halfSpace * 2;
            int right = left + halfSpace * 2;
            int bottom = child.getBottom() + halfSpace * 2;
            mColorDrawable.setBounds(left, top, right, bottom);
            mColorDrawable.draw(c);
            left = child.getLeft();

            top = child.getBottom();

            right = child.getRight();

            bottom = top + halfSpace * 2;

            mColorDrawable.setBounds(left, top, right, bottom);
            mColorDrawable.draw(c);
        }
    }
}